package com.owc.common.tool;

import com.owc.common.enums.CallbackTypeEnum;
import com.owc.common.enums.ExceptionEnum;
import com.owc.common.exception.BadRequestException;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author Jackies
 * @Date 2019/12/2 22:32
 * @Description TODO://
 **/
public class UtilsService {

    /**
     * Get six random number
     */
    public static Integer genRandom(){
        return (int) ((Math.random() * 9 + 1) * 100000);
    }

    /**
     * Get uuid.
     */
    public static String getGuid(){
        return UUID.randomUUID().toString();
    }

    /**
     * Determines whether the string is all Numbers
     */
    public static boolean isNumeric(String str) {
        // Regular expressions can match all Numbers including negative Numbers
        Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;//throw err!! description is not number str
        }
        Matcher isNum = pattern.matcher(bigStr);
        return isNum.matches();
    }

    public static <T> boolean isEmpty(T arg) {
        try {
            return Optional.of(arg)
                    .map(Object::toString)
                    .map(String::trim)
                    .map(c -> c.equals("null"))
                    .map(b -> b || arg.equals(""))
                    .map(a -> a || arg.equals("undefined"))
                    .orElse(false);
        } catch (Exception e) {
            return true;
        }
    }

    public static <T> T assertNotNull(Optional<T> optional, ExceptionEnum exceptionEnum){
        if(!optional.isPresent()){
            throw new BadRequestException(exceptionEnum);
        }
        return optional.get();
    }

    public static boolean isEth(String tokenName){
        return "ETH".equals(tokenName);
    }

    public static String judgeDrawType(String type){
        return CallbackTypeEnum.DRAW_IN_TYPE.getType().equals(type) ? "1" : "2";
    }

    /**
     * 获取字符串真实长度 (中文 = 2个字符)
     */
    public static int String_length(String value) {
        int valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }

    /**
     * 字符串格式化(占位符填充, 占位符: {}, 占位符可以自定义)
     */
    public static String stringFormat(String origin, String... args) {
        StringBuilder result = new StringBuilder(origin);
        String rep = "{}";
        for (String arg : args) {
            int start = result.indexOf(rep);
            int end = start + rep.length();
            result.replace(start, end, arg);
        }
        return result.toString();
    }

    /**
     * 提取#{****}内的值
     */
    public static String stringExtract(String str,String pattern){
        Assert.notNull(pattern,"Extract pattern must not be null.");
        Pattern regex = Pattern.compile(pattern);
        Matcher matcher = regex.matcher(str);
        while(matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

}
